// 订单模型
const mongoose = require('./db')
const Schema = mongoose.Schema

let Order = new mongoose.Schema({
  uid: { type: Schema.Types.ObjectId, required: true, ref: 'user', display: '用户ID' },
  address: { type: Schema.Types.ObjectId, required: true, display: '收货地址ID' },
  tips: { type: String, required: false, display: '订单备注' },
  product: { type: [{
    _id: { type: Schema.Types.ObjectId, required: true, display: '商品ID' },
    coupon: { type: Schema.Types.ObjectId, required: true, ref: 'coupon', display: '折扣ID' }, // 里面包含有折扣率 basePrice * 折扣率 = price
    basePrice: { type: Number, required: true, display: '商品原价' }, // <----> product.price
    price: { type: Number, default: 0.0, required: true, display: '付款单价' },
    count: { type: Number, default: 1, required: true, display: '商品数量' }
  }], required: true, display: '包含商品' },
  basePrice: { type: Number, required: true, default: 0.0, display: '订单总价'},
  coupon: { type: Schema.Types.ObjectId, required: true, ref: 'coupon', display: '折扣ID' },
  price: { type: Number, required: true, default: 0.0, display: '付款总价' },
  state: { type: Number, required: true, default: 0, display: '订单状态[0:未付款, 1:未发货, 2: 未签收, 3: 未确认, 4: 未评价, 5: 已评价]' }, // 付款的时候才生成
  payment: {
    payway: { type: Schema.Types.ObjectId, required: true, ref: 'payway', display: '付款方式' },
    payNumber: { type: String, required: true, default: '', display: '付款单号' },
  }
}, {
  collection: 'order',
  timestamps: {
    createAt: true,
    updateAt: true,
  }
})

let order = mongoose.model('order', Order)


// 折扣模型
let Coupon = new mongoose.Schema({
  name: { type: String, required: true, default: '默认折扣', display: '折扣名称' },
  count: { type: Number, required: true, default: 100, display: '折扣数量' },
  rate: { type: Number, required: true, default: 100, display: '折扣百分比' },
  condition: { type: String, required: true, default: 'price,up,0.0', display: '使用条件'},
  apply: { type: Number, required: true, default: 0, display: '适用类别[0:商品, 1:店铺, 2:商城]' },
  // createBy: { type: Schema.Types.ObjectId, required: true, ref: 'user', display: '创建者ID' },
}, {
  collection: 'coupon',
  timestamps: {
    createAt: true,
    updateAt: true,
  }
})

let coupon = mongoose.model('coupon', Coupon)


module.exports = {
  order: order,
  coupon: coupon,
}